﻿Imports System.Data
Imports System.Collections.ObjectModel
Imports System.ComponentModel

''' <summary>
''' Stellt eine Hilfklasse für einen bestehenden oder neuen Artikel dar
''' </summary>
''' <remarks></remarks>
Public Class Article
    Implements INotifyPropertyChanged

    Private _description, _no, _text As String
    Private _category As String = ""
    Private _taxclass As TaxClass
    Private _price1 As New EMoney(0) : Private _price2 As New EMoney(0) : Private _price3 As New EMoney(0)
    Private _unint As Unit
    Private _weight As Decimal
    Private _id As String = ""
    Private xml As IDatabase

    Public Property ArticleID() As String
        Get
            Return _no
        End Get
        Set(ByVal value As String)
            _no = value
            NotifyPropertyChanged("ArticleID")
        End Set
    End Property

    Public Property ArticleDescription() As String
        Get
            Return _description
        End Get
        Set(ByVal value As String)
            _description = value
            NotifyPropertyChanged("ArticleDescription")
        End Set
    End Property


    Public Property TaxClass() As TaxClass
        Get
            Return _taxclass
        End Get
        Set(ByVal value As TaxClass)
            _taxclass = value
            NotifyPropertyChanged("TaxClass")
        End Set
    End Property

    Public Property ArticleWeight As Decimal
        Get
            Return _weight
        End Get
        Set(value As Decimal)
            _weight = value
            NotifyPropertyChanged("ArticleWeight")
        End Set
    End Property

    Public Property WeightString As String
        Get
            Return ArticleWeight.ToString("0.000")
        End Get
        Set(value As String)
            ArticleWeight = CDec(value.Replace(".", ","))
        End Set
    End Property


    Public Property ArticleText() As String
        Get
            Return _text
        End Get
        Set(ByVal value As String)
            _text = value
            NotifyPropertyChanged("ArticleText")
        End Set
    End Property

    ''' <summary>
    ''' Represents 
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Property ArticlePrice1 As EMoney
        Get
            Return _price1
        End Get
        Set(ByVal value As EMoney)
            _price1 = value
            NotifyPropertyChanged("ArticlePrice1")
        End Set
    End Property

    Public Property ArticlePrice2 As EMoney
        Get
            Return _price2
        End Get
        Set(ByVal value As EMoney)
            _price2 = value
            NotifyPropertyChanged("ArticlePrice1")
        End Set
    End Property

    Public Property ArticlePrice3 As EMoney
        Get
            Return _price3
        End Get
        Set(ByVal value As EMoney)
            _price3 = value
            NotifyPropertyChanged("ArticlePrice1")
        End Set
    End Property

    Public Property ArticleUnit As Unit
        Get
            Return _unint
        End Get
        Set(ByVal value As Unit)
            _unint = value
            NotifyPropertyChanged("ArticleUnit")
        End Set
    End Property

    Public Property ArticleCategory() As String
        Get
            Return _category
        End Get
        Set(ByVal value As String)
            _category = value
            NotifyPropertyChanged("ArticleCategory")
        End Set
    End Property

    Public Shared Function Exists(art As Article, ByRef xml As IDatabase, usr As User) As Boolean
        Return xml.ArticleExists(art.ArticleID, usr)
    End Function

    Public Shared Function Exists(ArtNr As String, ByRef xml As IDatabase, usr As User) As Boolean
        Return xml.ArticleExists(ArtNr, usr)
    End Function

    Public Overrides Function Equals(obj As Object) As Boolean
        Try
            Dim _art As Article = obj
            If (_art.ArticleID = ArticleID) Then
                Return True
            End If
        Catch ex As Exception : End Try
        Return False
    End Function

    Public Shared Function GenerateSampleArticles(loader As IDatabase) As List(Of Article)
        Dim art As New Article()
        With art
            .ArticleID = "musart1"
            .ArticleDescription = "Musterartikel"
            .ArticleUnit = Unit.GetByName("Stück", loader)
            .ArticleWeight = 1.2
            .ArticlePrice1 = New EMoney(12.0, True)
            .ArticleText = "Sonderpreis"
            .TaxClass = loader.ListOfTaxes(0)
        End With

        Dim art2 As New Article()
        With art2
            .ArticleID = "musart2"
            .ArticleDescription = "Musterartikel 2"
            .ArticleUnit = Unit.GetByName("Kilogramm", loader)
            .ArticleWeight = 1
            .ArticlePrice1 = New EMoney(24.99, True)
            .TaxClass = loader.ListOfTaxes(0)
        End With

        Dim art3 As New Article()
        With art3
            .ArticleID = "musart3"
            .ArticleDescription = "Musterartikel 3"
            .ArticleUnit = Unit.GetByName("Beutel", loader)
            .ArticleWeight = 0.5
            .ArticlePrice1 = New EMoney(9.9, False)
            .TaxClass = loader.ListOfTaxes(0)
        End With
        Dim output As New List(Of Article)
        output.Add(art)
        output.Add(art2)
        output.Add(art3)
        Return output
    End Function

    Protected Sub NotifyPropertyChanged(info As [String])
        RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(info))
    End Sub

    Public Event PropertyChanged(sender As Object, e As System.ComponentModel.PropertyChangedEventArgs) Implements System.ComponentModel.INotifyPropertyChanged.PropertyChanged

    Public Overrides Function ToString() As String
        Return ArticleID + vbCr + ArticleDescription + vbCr + ArticlePrice1.ToString
    End Function
End Class
